import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import vue from '@vitejs/plugin-vue'
// import VueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/dist/resolvers'
import Components from 'unplugin-vue-components/vite'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  return {
    root: process.cwd(),
    base: env.VITE_MODE === 'production' ? './' : '/',
    plugins: [
      // VueDevTools(),
      AutoImport({ 
        imports: ['vue', 'vue-router'],
        resolvers: [ElementPlusResolver()],
        dts: true, // 生成自动导入的类型声明文件
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      vue(),

    ],
    server: {
      host: '0.0.0.0',
      port: 3000,
      open: true,
      proxy: {
        '/mock': env.VITE_BASE_API,
        '/api': {
          target: env.VITE_BASE_API,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        },
        '/socket.io': {
          target: 'ws://localhost:5174',
          ws: true
        }
      }
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '@style': path.resolve(__dirname, './src/assets/style'),
        '@images': path.resolve(__dirname, './src/assets/images')
      },
      extensions: ['.mjs', '.js', '.jsx', '.json']
    },
    build: {
      outDir: "dist",
      assetsDir: "assets",
      sourcemap: false,
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      }
    }
  }
})